//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Windows.Media;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet
{
    public class ThemeColor : SpreadsheetColor
    {
       /* public ThemeColorReference(ColorSchemeIndex colorIndex, double? tint)
        {
            this.colorIndex = colorIndex;
            Tint = tint;
        }*/
        public ThemeColor(ColorSchemeIndex colorIndex)
            : this(colorIndex, null)
        {
        }

        public ThemeColor(ColorSchemeIndex colorIndex, double? tint)
        {
            Contract.Requires(Math.Abs(tint.GetValueOrDefault(0)) <= 1);
            _colorIndex = colorIndex;
            _tint = tint;
        }


        private readonly ColorSchemeIndex _colorIndex;
        private double? _tint;

        public ColorSchemeIndex ColorIndex
        {
            get { return _colorIndex; }
        }

        public double? Tint
        {
            get { return _tint; }
        }


        public override Color GetColor(ColorScheme colorScheme)
        {
           //\\ Contract.Requires(colorScheme != null);
            ColorSchemeIndex i = ColorIndex;
            if (colorScheme == null)
                return Colors.Pink;
            ColorBase d = colorScheme[i];
            //\\Color c = d.Brush.Color;
            Color c = d.ResolveColor(colorScheme);
            if (Tint != null)
            {
                c = TintTransform.CalculateTint(c, Tint.Value);
            }
            return (c);
        }


        
        public override int GetHashCode()
        {
            return (int)ColorIndex + Tint.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            ThemeColor other = obj as ThemeColor;
            if (other == null)
                return false;
            return other.ColorIndex == ColorIndex && Equals(Tint.GetValueOrDefault(0), other.Tint.GetValueOrDefault(0));
        }


        internal override void WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.SetInteger("theme", (int)ColorIndex);
                if (Tint != null)
                    c.SetDouble("tint", Tint.Value);
            }
        }
    }
}
